#include "starSystem.hpp"
#include "basePosition.hpp"

int starSystem::globalID = 0;

starSystem::starSystem()
{
	localID = globalID;
	globalID++;
}

void starSystem::addChild( std::shared_ptr<basePosition> childPtr )
{
	// should check that direct children are fixed
	childPtr->setSys(self);
	childList.push_back( childPtr );
}

void starSystem::tick(int64_t timeStep)
{
	// general system stuff here

	// consider having kickList keep shared_ptrs rather than weak_ptrs for consistency
	kickList.clear();
	for(auto it = childList.begin(); it != childList.end(); it++)
	{
		(*it)->tick(timeStep);
	}

	for(auto it = kickList.begin(); it != kickList.end(); it++)
	{
		std::cout << "kicking " << (*it)->plocalID << "\n";
	}

	for(auto it = kickList.begin(); it != kickList.end(); it++)
	{
		auto par = (*it)->getParent().lock();
		(*it)->setParentw(par->getParent());
		(*it)->setState( (*it)->getState() + par->getState() );
//		(*it)->setMove(move_t::FIXED);
	}
}

void starSystem::flushState()
{
	size_t childNum = childList.size();
	for(size_t i = 0; i < childNum; i++)
	{
		childList[i]->flushState();
	}
}

void starSystem::refreshDisp()
{
	size_t childNum = childList.size();
	for(size_t i = 0; i < childNum; i++)
	{
		childList[i]->refreshDisp();
	}
}

void starSystem::draw(sf::RenderWindow * window)
{
	// probably put some shit about a ui in here

	size_t childNum = childList.size();
	for(size_t i = 0; i < childNum; i++)
	{
		childList[i]->draw(window);
	}
}
